/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,RequestService,$state,$stateParams,modelService,$localStorage,commonConfig) {
    $scope.User = $localStorage.get('cloud');
    $scope.editFlag = false;
    $scope.NameTip = '';
    //获取parentId列表
    RequestService.Request(commonConfig.cloudApi+'wiki/getWikiSelectList/0','GET').then(function(response){
        console.log(response);
        $scope.parentIdList = [{wikiName:'默认', id:0}];
        if(response.length > 0){  //若返回数据为空
            $scope.parentIdList = $scope.parentIdList.concat(response);
        }
        // console.log($scope.parentIdList);
        //初始化
        $scope.form = {
            wikiName:'',
            wikiNo:0,
            remark:'',
            parentId: $scope.parentIdList[0].id //默认选中第一个
        }
        // $scope.form.parentId = $scope.parentIdList[0].id;  //默认选中第一个

    },function(error){
        console.log(error);
    });

   // //判断是否有传值
   //  if($stateParams.id){
   //     // console.log($stateParams.id);
   //      //获取单条数据
   //      httpfactory.getWiki($stateParams.id,$scope.User.token).then(function(response){
   //          console.log(response);
   //          $scope.form = {
   //              wikiName:response.data.wikiName,
   //              wikiNo:response.data.wikiNo,
   //              remark:response.data.remark,
   //              parentId:response.data.parentId
   //          }
   //      },function(error){
   //          console.log(error);
   //      });
   //  }
    $scope.ParentIdChange = function(parentId){
        $scope.form.parentId = parentId;
    };
   //提交数据
    $scope.submit = function(){
        console.log($scope.form);
        var data={
            "id" : 0,  // 添加默认为0
            "parentId" : $scope.form.parentId,
            "remark" : $scope.form.remark,
            "wikiName" : $scope.form.wikiName,
            "wikiNo" : $scope.form.wikiNo
        };
        // if($stateParams.id){  //修改数据
        //     httpfactory.resetWiki($stateParams.id,$scope.form,$scope.User.token).then(function(response){
        //         console.log(response);
        //         if(response.data.code == 0){
        //             var page = 'DeveloperManager.HelpManage';
        //             modelService.openTipsModel('修改成功！',page);
        //             $scope.form = {
        //                 wikiName:'',
        //                 wikiNo:0,
        //                 remark:'',
        //                 parentId: $scope.parentIdList[0].id //默认选中第一个
        //             }
        //         }
        //     },function(error){
        //         console.log(error);
        //     });
        // }else {               //第一次提交
        //     httpfactory.addWiki($scope.form,$scope.User.token).then(function(response){
        //         console.log(response);
        //         if(response.data.code == 0){
        //             var page = 'DeveloperManager.HelpManage';
        //             modelService.openTipsModel('提交成功！',page);
        //             $scope.form = {
        //                 wikiName:'',
        //                 wikiNo:0,
        //                 remark:'',
        //                 parentId: $scope.parentIdList[0].id //默认选中第一个
        //             }
        //         }
        //     },function(error){
        //         console.log(error);
        //     });
        // }
        RequestService.Request(commonConfig.cloudApi+'wiki','POST',data).then(function(response){
            console.log(response);
            if(response.code == 0){
                var page = 'DeveloperManager.HelpManage';
                modelService.openTipsModel('提交成功！',page);
                $scope.form = {
                    wikiName:'',
                    wikiNo:0,
                    remark:'',
                    parentId: $scope.parentIdList[0].id //默认选中第一个
                }
            }
        },function(error){
            console.log(error);
        });

    };

    //检测标题是否重名
    $scope.CheckName = function(){
        if($scope.form.wikiName != undefined){
            console.log($scope.form.wikiName);
            RequestService.Request(commonConfig.cloudApi+'wiki/exists/'+$scope.form.wikiName,'GET').then(function(response){
                console.log(response);
                if(response == 'true'){
                    $scope.NameTip = '可使用标题';
                }else {
                    $scope.NameTip = '此标题已存在!';
                }

            },function(error){
                console.log(error);
            })
        }
    };

    // function openModel(content,page){
    //     $modal.open({
    //         animation: true,
    //         templateUrl: './views/model/alertModel.html',
    //         size: 'sm',
    //         controller: function($scope,$modalInstance,$timeout,$state) {
    //             $scope.content = content;
    //             $timeout(function(){
    //                 $modalInstance.close();
    //                 $state.go(page);
    //             },1000);
    //         }
    //     });
    // }

    
};